<template>
  <div>
    <div
      style="position: relative"
      @mouseenter="menu = true"
      @mouseleave="menu = false"
      class="el-upload"
    >
      <img v-if="data" :src="data" class="avatar" />
      <el-icon v-else :class="['avatar-uploader-icon', hidePlus ? 'icon-content' : '']"
      ><Plus v-if="!hidePlus"
      /></el-icon>
      <div
        class="upload-actions"
        v-if="menu && data"
        @click.stop="
          () => {
            return false
          }
        "
      >
        <el-icon class="action-icon" @click.stop="handlePreview"><Search /></el-icon>
        <el-icon class="action-icon" v-if="!disabled" @click.stop="handleDelete"
        ><Delete
        /></el-icon>
      </div>
    </div>
    <Teleport to="body">
      <el-image-viewer
        v-if="showPreview"
        hide-on-click-modal
        @close="previewClose"
        :url-list="previewList"
        :z-index="9999"
        :initial-index="0"
      />
    </Teleport>
  </div>
</template>

<script>
import ElImageViewer from 'element-plus/es/components/image-viewer/index'

export default {
  name: 'ImgPreview',
  components: { ElImageViewer },
  // model: {
  //   prop: 'data',
  //   event: 'changeValue'
  // },
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    disabled: {
      type: Boolean,
      default: false
    },
    hidePlus: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showPreview: false,
      menu: false,
      previewList: [],
      loading: false
    }
  },
  watch: {
    data: {
      handler(newVal) {},
      immediate: true
    }
  },
  methods: {
    handlePreview() {
      this.showPreview = true
      this.previewList = [this.data]
    },
    previewClose() {
      this.showPreview = false
      this.previewList = []
    },
    handleDelete() {
      this.$emit('changeValue', '')
    }
  }
}
</script>

<style scoped>
.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.icon-content {
  display: block;
  cursor: default;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.upload-actions {
  position: absolute;
  z-index: 2;
  top: 0;
  width: 178px;
  height: 178px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  background-color: #8c939d;
  opacity: 0.8;
}

.action-icon {
  font-size: 20px;
  color: #fff;
}
</style>
